package pl.oczadly.jabberclient.message.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.packet.Message;

import pl.oczadly.jabberclient.message.presenter.IMessagePresenter;
import pl.oczadly.jabberclient.message.presenter.MessagePresenter;
import pl.oczadly.jabberclient.message.view.manager.MessageViewManager;
import pl.oczadly.jabberclient.utils.Fonts;
import pl.oczadly.jabberclient.utils.Utils;

@SuppressWarnings( "serial" )
public class MessageView extends JFrame {
	private String id;
	private String name;

	private IMessagePresenter presenter;

	private JCheckBox ignore;
	private JTextArea tA;
	private JTextField tf;

	private MessageListener messageListener = new MyMessageListener();
	private JButton sendButton;
	
	public MessageView( String id, String name ) {
		this.id = id;
		this.name = name;
		presenter = new MessagePresenter( this, id, name );
	}
	
	public void createUI() {
		presenter.addChatManager( messageListener );
		
		this.setTitle( name );
		addLayout( this );
		
		
		this.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
		this.setSize( 550, 300 );
		this.setVisible( true );
		
		MessageViewManager.addMessageView( this );
	}
	
	private void addLayout( JFrame frame ) {
		JPanel contactPanel = new JPanel( new FlowLayout( FlowLayout.LEADING ) );
		JLabel contactName = new JLabel( name );
		ignore = new JCheckBox( "Ignore" );
		ignore.addItemListener( new IgnoreListener() );
		contactPanel.add( contactName );
		contactPanel.add( ignore );
		frame.add( contactPanel, BorderLayout.NORTH );
		
		tA = new JTextArea();
		tA.setFont( Fonts.MESSAGE_FONT );
		tA.setEditable( false );
		JScrollPane scroller = new JScrollPane( tA );
		scroller.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
		scroller.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED );

		frame.add( scroller, BorderLayout.CENTER );
		
		JPanel bottomPanel = new JPanel();
		tf = new JTextField( 50 );
		tf.addActionListener( new SendMessageListener() );
		sendButton = new JButton( "Send" );
		sendButton.addActionListener( new SendMessageListener() );
		bottomPanel.add( tf );
		bottomPanel.add( sendButton );
		frame.add( bottomPanel, BorderLayout.SOUTH );
		
	}
	
	public void setMessage( String from, String body ) {
		tA.setText( tA.getText() + from + " " + getCurrentDate() + ": " + "\n" + body + "\n" );
	}
	
	private String getCurrentDate() {
        DateFormat dateFormat = new SimpleDateFormat( "dd/MM/yyyy HH:mm:ss" );
        Date date = new Date();
        return dateFormat.format( date );
    }
	
	@Override
	public void dispose() {
		MessageViewManager.removeMessageView( this );
		
		super.dispose();
	}
	
	public String getId() {
		return id;
	}
	
	public String getTitleName() {
		return name;
	}
	
	private class IgnoreListener implements ItemListener {

		public void itemStateChanged( ItemEvent e ) {
			if( ignore.isSelected() ) {
				presenter.ignore( messageListener );
				tf.setEnabled( false );
				sendButton.setEnabled( false );
			}
			else {
				presenter.addChatManager( messageListener );
				tf.setEnabled( true );
				sendButton.setEnabled( true );
			}
		}
		
	}
	
	private class SendMessageListener implements ActionListener {

		public void actionPerformed( ActionEvent e ) {
			String messageContent = tf.getText();
			presenter.sendMessage( messageContent );
			
			setMessage( Utils.getLogin(), messageContent );
			tf.setText( "" );
		}
		
	}
	
	private class MyMessageListener implements MessageListener {

		public void processMessage( Chat chat, Message message ) {
			String body = message.getBody();
			setMessage( name, body );
		}
		
	}
	
}
